<template>
<el-container>
  <el-header style="height: auto;">
    <el-row>
      <el-col :xl="{ span: 4, push: 1 }">
        <h2></h2>
      </el-col>
    </el-row>
  </el-header>
  <el-main>
    <el-form :model="form" ref="form" :rules="formRules">
      <el-row>
        <el-col class="box" :xl="{ span: 6, push: 1 }" :lg="{ span: 6, push: 1 }">
          <el-form-item label="客户标识" label-width="80px" prop="name">
            <el-input size="small" v-model="form.name" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="box" :xl="{ span: 6, push: 1 }" :lg="{ span: 6, push: 1 }">
          <el-form-item label="联系人" label-width="80px" prop="contacts">
            <el-input size="small" v-model="form.contacts" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="box" :xl="{ span: 6, push: 1 }" :lg="{ span: 6, push: 1 }">
          <el-form-item label="联系电话" label-width="80px" prop="phone">
            <el-input size="small" v-model="form.phone" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="box" :xl="{ span: 6, push: 1 }" :lg="{ span: 6, push: 1 }">
          <el-form-item label="联系地址" label-width="80px" prop="address">
            <el-input size="small" v-model="form.address" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col class="box" :xl="{ span: 6, push: 1 }" :lg="{ span: 6, push: 1 }">
          <el-form-item label-width="90px" prop="channelCompanyId">
            <el-button size="small" type="primary" @click="ok('form')" :loading="sub">提交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-main>
</el-container>
</template>

<script>
import { ordinaryUsersSave } from '@/api/getData'

export default {
  name: 'OrdinaryUserAdd',

  data() {
    const validateName = (rule, value, callback) => {
      if (value.length > 60) {
        callback(new Error('用户标识不能超过60位'))
      } else {
        callback()
      }
    }
    const validateAddress = (rule, value, callback) => {
      if (value.length > 200) {
        callback(new Error('联系地址不能超过200位'))
      } else {
        callback()
      }
    }
    const validateContacts = (rule, value, callback) => {
      if (value.length > 20) {
        callback(new Error('联系人不能超过20位'))
      } else {
        callback()
      }
    }
    return {
      sub: false,
      formRules: {
        name: [
          { required: true, message: '请填写客户标识', trigger: 'blur' },
          { validator: validateName }
        ],
        phone: [
          { required: true, message: '请填写联系电话', trigger: 'blur' },
          { pattern: /^\d{1,18}$/, message: '联系电话必须是数字且不能超过18位', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请填写联系地址', trigger: 'blur' },
          { validator: validateAddress }
        ],
        contacts: [
          { required: true, message: '请填写联系人', trigger: 'blur' },
          { validator: validateContacts }
        ]
      },
      form: {
        name: '',
        phone: '',
        address: '',
        contacts: ''
      }
    }
  },
  methods: {
    ok(form) {
      this.$refs[form].validate(async valid => {
        if (valid) {
          const a = await ordinaryUsersSave({
            name: this.form.name,
            phone: this.form.phone,
            address: this.form.address,
            contacts: this.form.contacts
          },
          {
            obj: this,
            key: 'sub'
          })
          if (a._result) {
            this.sub = false
            this.$message.success('添加成功')
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
